package com.petadoption.serverclient.controller;

import com.petadoption.serverclient.service.IUserService;
import com.petadoption.serverclient.utils.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileController {
    @Resource
    private IUserService userService;
    @Resource
    private Result result;

    private final String savePath = "D:\\Nginx\\nginx-1.24.0\\html\\images\\";

//    private static String IP;
//
//    static {
//        try {
//            String localIP = IpUtil.getLocalIP();
//            IP = localIP;
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//
//    }

    private static String IPService = "http://172.30.1.163/images";

    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestPart("file") MultipartFile upAvatar) throws IOException {


        // 获取原始文件名
        String oriName = upAvatar.getOriginalFilename();
        // 判断目录是否存在并创建
        File rootDir = new File(savePath + "avatars");
        if (rootDir.exists() == false) {
            rootDir.mkdirs();
        }
        // 防止文件名冲突，用uuid随机生成文件名，拼接文件类型后缀
        String newName = UUID.randomUUID().toString() + oriName.substring(oriName.lastIndexOf("."), oriName.length());
        File saveFile = new File(rootDir, newName);
        //转存到指定文件中
        upAvatar.transferTo(saveFile);
        // *文件绝对路径，保存在数据库
//         String avatar = saveFile.getAbsolutePath();
        // 文件的相对路径，保存在数据库
        String relativePath = "/avatars/";
        String avatar = IPService + relativePath + newName;
        return result.success("头像上传成功", avatar);
    }

    @RequestMapping("/petimg")
    public Result updatePet(@RequestPart("file") MultipartFile upAvatar) throws IOException {
        //获取原始文件名
        String oriName = upAvatar.getOriginalFilename();
        // 判断目录是否存在并创建
        File rootDir = new File(savePath + "petImg");
        if (rootDir.exists() == false) {
            rootDir.mkdirs();
        }
        // 防止文件名冲突，用uuid随机生成文件名，拼接文件类型后缀
        String newName = UUID.randomUUID().toString() + oriName.substring(oriName.lastIndexOf("."), oriName.length());
        File saveFile = new File(rootDir, newName);
        //转存到指定文件中
        upAvatar.transferTo(saveFile);
        // *文件绝对路径，保存在数据库
//         String avatar = saveFile.getAbsolutePath();
        // 文件的相对路径，保存在数据库
        String relativePath = "/petImg/";
        String petImg = IPService + relativePath + newName;
        return result.success("宠物图片上传成功", petImg);
    }

//    @RequestMapping("/upload")
//    public Result updatePet(@RequestPart("file") MultipartFile upAvatar) throws IOException {
//

//        String originalFilename = upAvatar.getOriginalFilename();
//        byte[] bytes = upAvatar.getBytes();
//        String name = upAvatar.getName();
//        String contentType = upAvatar.getContentType();
//        System.out.println("originalFilename:" + originalFilename);
//        System.out.println("contentType = " + contentType);
//        System.out.println("name = " + name);
//        System.out.println("bytes = " + bytes);
//
//        return result.success("宠物图片上传成功", upAvatar.getOriginalFilename());
//    }

}